package co.fitstart.admin.entity.content;

import java.util.Map;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

/**
 * 
 * @author downpour
 */
public class NewsSearch implements QueryBuilder {

    private String name;
    
    private Order order;
    
    private Filter filter;
    
    /**
     * The default constructor
     */
    public NewsSearch() {
        
         order = new Order("serial", "ASC")
                      .add("createTime", "DESC");
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
         if(filter == null || refresh) {
                filter = new Filter().addLike("name", name)                                     
                                     .addEquals("disabled", false);
            }

            return filter;
    }    

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        
        return "FROM News " + getPreparedFilter(false).getWhereSentence() + order.getSentence();
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        
        return getPreparedFilter(false).getParameters();
    }

    /**
     * 
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * 
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return order;
    }

    /**
     * 
     * @param order the order to set
     */
    public void setOrder(Order order) {
        this.order = order;
    }

}
